package com.UdpRecieve.utils;

import org.dom4j.*;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class XMLUtils {


    /**
     * 仅支持两层
     * @param xml
     * @return
     * @throws DocumentException
     */
    public static Map<String,String> readLabelXMLObjectSDYX(String xml) throws DocumentException {
        Document document = DocumentHelper.parseText(xml);
        // 获取根结点对象
        Element rootElement = document.getRootElement();

        Map<String,String> hashMap = new HashMap<String,String>();

        // 循环根节点，获取其子节点
        for (Iterator iter = rootElement.elementIterator(); iter.hasNext();) {
            Element element = (Element) iter.next(); // 获取标签对象
            // 获取该标签对象的名称
            String tagName1 = element.getName();
            // 获取该标签对象的内容
            String tagContent1 = element.getTextTrim();
            if (tagName1 == "secrect"){
                hashMap.put("secrect",tagContent1);
            }
            if (tagName1 == "status"){
                hashMap.put("status",tagContent1);
            }
            hashMap.put(tagName1,tagContent1);
            // 循环第一层节点，获取其子节点
            for (Iterator iterInner = element.elementIterator(); iterInner.hasNext();) {
                // 获取标签对象
                Element elementOption = (Element) iterInner.next();
                // 获取该标签对象的名称
                String tagName = elementOption.getName();
                // 获取该标签对象的内容
                String tagContent = elementOption.getTextTrim();

                if (tagName1 == "draftsMan"&&tagName=="name"){
                    tagContent = tagContent.contains("]")?tagContent.split("\\[")[2].split("]")[0]:tagContent;
                    hashMap.put("draftsMan",tagContent);
                }
                if (tagName1 == "secretMan"&&tagName=="oid"){
                    tagContent = tagContent.contains("]")?tagContent.split("\\[")[2].split("]")[0]:tagContent;
                    hashMap.put("secretMan",tagContent);
                }
                if (tagName1 == "extensions"){
                    for(Iterator iterInner3 = elementOption.elementIterator(); iterInner3.hasNext();){
                        // 获取标签对象
                        Element elementOption3 = (Element) iterInner3.next();
                        // 获取该标签对象的名称
                        String tagName3 = elementOption3.getName();
                        // 获取该标签对象的内容
                        String tagContent3 = elementOption3.getTextTrim();
                        if (tagName3=="critical"){
                            hashMap.put("critical",tagContent3);
                        }
                    }
                }
            }
        }
        return hashMap;
    }

}
